package com.lc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Author:LC
 * Version:jdk-8u301
 * Date: 2022/7/14 17:44
 */
@RestController
public class DiscoveryController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/discovery")
    public String discovery01(String serviceName){
        //服务发现，通过服务的名称拿到服务信息，需要将配置文件中fetch-registry设置为true，打开检索服务功能
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);

        instances.forEach(System.out::println);

        return instances.get(0).toString();
    }

}
